<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	pageContext.setAttribute("path",path);
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<script type="text/javascript">
$(".permission_cont").off(".check");

$(".permission_cont").on("click.check","input[type='checkbox']",{},function(){
	//向上检查
	var container = $(this).closest("div.permission_container");
	var checks= container.children("label").find("input[type='checkbox']");// input[type='checkbox']
	var checkeds = container.children("label").find("input[type='checkbox']:checked");
	if( checks.length == checkeds.length){
		var parent = container.parent().closest("div.permission_container");
		if(parent){
			parent.children("label").find("input[type='checkbox']").attr("checked","checked");
		}
	}else{
		var parent = container.parent().closest("div.permission_container");
		if(parent){
			parent.children("label").find("input[type='checkbox']").attr("checked",false);
		}
	}
	
	//向下检查
	var childContainer = container.children("div.permission_container");
	if( childContainer ){
		var c = container.children("label").find("input[type='checkbox']");
		if( c.is(":checked") ){
			childContainer.find("input[type='checkbox']").attr("checked","checked");
		}else{
			childContainer.find("input[type='checkbox']").attr("checked",false);
		}
	}
});

$(function(){
	$(".easyui-linkbutton").linkbutton({});
	//选中已有的权限 -start
	var ps = "${ps}";
	var div = $('#rolegroup_datagrid').datagrid("findExtendContent_x","${index}");
	if( div && ps!=null && ps.length > 0 ){
		var p_array = ps.split(",");
		var checks = div.find("input[name='permission_keys']");
		checks.each(function(){
			var v = $(this).val();
			if( jQuery.inArray(v, p_array) != -1 ){
				$(this).attr("checked", "checked");
			}
		});
	}
	//选中已有的权限 -end
});

</script>

<div style="padding:5px 0 5px 20px;background-color:#ccc">
	<form >
		<input type="hidden" name="rolegroup_id" value="${rolegroup_id}" />
		<div class="permission_cont" >
			<c:forEach items="${list}" var="data" >
				<div class="permission_container" style="font-size:12px;border-bottom:1px solid gray;padding:5px 0 3px 0">
					<label><input type="checkbox" value="${data.key}" name="permission_keys" style="vertical-align:-3px" />&nbsp;<span>${data.name}</span></label>
					<div class="permission_container" >
						<c:forEach items="${data.children}" var="child" >
						<label><input type="checkbox" value="${child.key}" name="permission_keys" style="vertical-align:-3px" />&nbsp;<span>${child.name}</span></label>
						</c:forEach>
					</div>
				</div>
			</c:forEach>
		</div>
		<div style="margin:5px 0 0 0">
			<a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="RoleGroup.savePermission( this );">保存分配</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="RoleGroup.closeDatagridExtend( this );">关闭</a>
		</div>
	</form>
</div>